package controllers

import (
	"fox/common"
	"fox/enum/code"
	"fox/enum/fields"
	"fox/models"
	"fox/repositories"
	request2 "fox/request"
	response2 "fox/response"
	"fox/validators"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type RoleController struct {
	repository *repositories.RoleRepository
}

func NewRoleController() (roleController *RoleController) {

	return &RoleController{repository: repositories.NewRoleRepository()}

}

func (controller *RoleController) SaveRole(c *gin.Context) {
	var (
		saveRole *request2.SaveRole
		role     *models.Role
		id       int64
		err      error
	)

	saveRole = &request2.SaveRole{}
	if err = c.ShouldBind(&saveRole); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, validators.Translate(err), ""))
		return
	}

	role = &models.Role{
		Name:       saveRole.Name,
		CreateTime: common.DateTimeNowFormat(),
		UpdateTime: common.DateTimeNowFormat(),
		Status:     fields.StatusOk,
	}

	if id, err = controller.repository.InsertRole(role); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.QueryError, code.QueryErrorStr, ""))
		return
	}

	c.JSON(http.StatusOK, response2.Response(code.Success, code.SuccessStr, id))
	return
}

func (controller *RoleController) GetRole(c *gin.Context) {
	var (
		idStr string
		idInt int
		role  *models.Role
		err   error
	)

	idStr = c.Query("id")
	if idStr == "" {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, code.ParameterErrorStr, ""))
		return
	}
	if idInt, err = strconv.Atoi(idStr); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, err.Error(), ""))
		return
	}
	if role, err = controller.repository.SelectRoleById(int64(idInt)); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.QueryError, err.Error(), ""))
		return
	}
	c.JSON(http.StatusOK, response2.Response(code.Success, code.SuccessStr, role))
	return

}

func (controller *RoleController) DeleteRole(c *gin.Context) {
	var (
		idStr string
		idInt int
		err   error
	)

	idStr = c.Query("id")
	if idStr == "" {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, code.ParameterErrorStr, ""))
		return
	}

	if idInt, err = strconv.Atoi(idStr); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, err.Error(), ""))
		return
	}

	if err = controller.repository.DeleteRoleById(int64(idInt)); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.QueryError, err.Error(), ""))
		return
	}

	c.JSON(http.StatusOK, response2.Response(code.Success, "删除成功", ""))
	return
}

func (controller *RoleController) UpdateRole(c *gin.Context) {
	var (
		updateRole *request2.UpdateRole
		role       *models.Role
		roleNew    *models.Role
		err        error
	)

	updateRole = &request2.UpdateRole{}
	if err = c.ShouldBind(&updateRole); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, validators.Translate(err), ""))
		return
	}

	role = &models.Role{
		ID: updateRole.ID,
	}
	if len(updateRole.Name) > 0 {
		role.Name = updateRole.Name
	}
	if updateRole.Status != fields.StatusOk && updateRole.Status != fields.StatusNo {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, "status有误", ""))
		return
	}
	role.Status = updateRole.Status

	if roleNew, err = controller.repository.UpdateRoleById(role); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.QueryError, err.Error(), ""))
		return
	}

	c.JSON(http.StatusOK, response2.Response(code.Success, code.SuccessStr, roleNew))
	return
}

func (controller *RoleController) GetRoles(c *gin.Context) {
	var (
		role  *request2.Role
		Name  string
		roles *response2.Result
		ok    bool
		err   error
	)

	role = &request2.Role{}
	if role.Page, err = strconv.Atoi(c.DefaultQuery("page", "1")); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, err.Error(), ""))
		return
	}
	if role.Size, err = strconv.Atoi(c.DefaultQuery("size", "10")); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, err.Error(), ""))
		return
	}
	role.Sort = c.DefaultQuery("sort", fields.DESC)
	if role.Sort != fields.DESC && role.Sort != fields.ASC {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, "排序有误", ""))
		return
	}
	role.SortField = c.DefaultQuery("sortField", fields.CreateTime)
	if ok = request2.ValidateRoleSortFile(role.SortField); ok != true {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, "排序参数有误", ""))
		return
	}
	Name = c.Query("Name")
	if Name != "" {
		role.Name = Name
	}
	if roles, err = controller.repository.SelectRoles(role); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.QueryError, err.Error(), ""))
		return
	}
	c.JSON(http.StatusOK, response2.Response(code.Success, code.SuccessStr, roles))
	return
}
